
 import pick from 'lodash.pick'
 import { getAction, putAction,postAction,downFile,httpAction } from '@/api/manage'
 import debounce from 'lodash/debounce';
const yaodianFormMinxin = {
  data(){
    this.voidRepeat = debounce(this.voidRepeat,500)
    return {
       
        // 最多显示通知的数量
       maxNotifications: 3
       // 当前显示的通知数量
       ,currentNotifications: 0,
       isChaiLing:false,
       isNoSym:false
    }
  },
  destroyed(){
    //删除 之前路由里所有的 提示弹窗
    this.clearAllWarining()
  },
  computed: {
    
  },
  created(){
   
  },
  methods:{
    noSymChange(val){
      console.log(val,'123')
      if(val == 1){
        this.isNoSym = true
      }else{
        this.isNoSym = false
      }

      
    },
    RatioFlagChange(val){
      console.log(val.target.checked,'222')
      if(val.target.checked){
        this.$nextTick(()=>{
          this.$refs.jcbl.focus()
        })
        
      }
    },
    chailingChange(val){
      var that = this;
      console.log(val)
      if(val == 1){
        this.isChaiLing = true
      }else{
        this.isChaiLing = false
        this.form.setFieldsValue({'tracePackageQuantity':'1'})
      }
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          let formData = Object.assign(this.model, values);
          // formData.markupRatioFlag = values.markupRatioFlag ? '1' :'0'
          console.log("表单提交数据",formData)
          // return
           // 新增和编辑时，校验零售价，若高于限价信息，给出提示：零售价高于医保目录限价，是否继续
          that.viodInfo(formData)
         
        }
       
      })
    },
    // 保存药典的校验
    viodInfo(formvalue){
      var that = this;
      this.viodXianjia(formvalue)
      // return
      
        
    },
    viodXianjia(formvalue){
      var that = this;
      if(formvalue && formvalue.retailPrice && Number(this.model.lmtPrice)>0 &&  Number(formvalue.retailPrice)>  Number(this.model.lmtPrice)){
        this.$confirm({
          title: '零售价高于医保目录限价，是否继续?',
          icon:'exclamation-circle',
          // content: 'When clicked the OK button, this dialog will be closed after 1 second',
          okText:'继续',
          cancelText:'返回修改',
          onOk() {
              that.voidRepeat(formvalue)
          },
          onCancel() {},
        });
      }else{
         that.voidRepeat(formvalue)
      }
    },
    isObjectValuesNonEmpty(obj,requiredKeys) {  
      // 检查传入的参数是否是一个对象  
      if (typeof obj !== 'object' || obj === null) {  
        return false;  
      }  
      // 遍历requiredKeys数组  
      for (const key of requiredKeys) {  
        // 检查key是否存在于对象中  
        if (!(key in obj)) {  
          return false; // 如果key不存在，则返回false  
        }  
        // 检查value的值是否为空  
        const value = obj[key];  
        if (value === null || value === undefined || value === '' || (Array.isArray(value) && value.length === 0)) {  
          return false; // 如果value为空，则返回false  
        }  
      }  
      // 如果所有value都非空，则返回true  
      return true;  
    },
    // 异步验证重复 val={target:{value:''}},field,name,flag = false
    voidRepeat(formvalue){
      var that = this;
      // 只在添加的时候限制 校验
      if(!this.model.id){
          var tips = '[药品名称 规格  厂家 ]'
          var params ={}
          if(that.repeatForm.project == '1'){
            // tips =  '[药品名称 规格  厂家 ]'
            tips =  `【${formvalue.name}，${formvalue.manufacturer}，${formvalue.specificationDescribe}】`
            params.name = formvalue.name
            params.manufacturer = formvalue.manufacturer
            params.specification= formvalue.specificationDescribe
          }
          if(that.repeatForm.project == '2' && !that.isObjectValuesNonEmpty(params,['name','originPlace'])){
            // tips =  '[药品名称 产地]'
             tips =  `【${formvalue.name}，${formvalue.originPlace}】`
            params.name = formvalue.name
            params.originPlace = formvalue.originPlace
          }
          if(that.repeatForm.project == '3' && !that.isObjectValuesNonEmpty(params,['name','manufacturer','specification'])){
            // tips =  '[药品名称 厂家 规格]'
            tips =  `【${formvalue.name}，${formvalue.specification}】`
            params.name = formvalue.name
            params.manufacturer = formvalue.manufacturer
            params.specification = formvalue.specification
          }
          if(that.repeatForm.project == '4' && !that.isObjectValuesNonEmpty(params,['name'])){
            // tips =  '[药品名称]'
            tips =  `【${formvalue.name}】`
            params.name = formvalue.name
           
          }
          postAction(this.url.repeatCheck,params).then((res)=>{
              if(res.success){
                if(res.result.repeatFlag +'' === '1'){
                  this.$confirm({
                    title: tips + '已存在，是否继续添加！',
                    icon:'exclamation-circle',
                    // content: 'When clicked the OK button, this dialog will be closed after 1 second',
                    okText:'继续',
                    cancelText:'返回修改',
                    onOk() {
                        that.fnUrl(formvalue)
                    },
                    onCancel() {},
                  });
                }else{
                  that.fnUrl(formvalue)
                }
              }
          });
      }else{
        that.fnUrl(formvalue)
      }
    },
    fnUrl(formData){
      var that = this;
      that.confirmLoading = true;
      let httpurl = '';
      let method = '';
      if(!this.model.id){
        httpurl+=this.url.add;
        method = 'post';
      }else{
        if(this.YXSdisableSubmit){
          httpurl+=this.url.saleEdit;
        }else{
          httpurl+=this.url.edit;
        }
        method = 'put';
      }
      // formData.markupRatioFlag = formData.markupRatioFlag?1:0
      httpAction(httpurl,formData,method).then((res)=>{
        if(res.success){
          that.$message.success(res.message);
          that.$emit('ok');
        }else{
          that.$message.warning(res.message);
          if(res.code+'' ==='544'){
            this.getCode();
          }
        }
      }).finally(() => {
        that.confirmLoading = false;
      })
    },
    clearAllWarining(){
      //删除 之前路由里所有的 提示弹窗
      $('.ant-notification .ant-notification-notice').remove();
      this.currentNotifications = 0
    },
    // 提示信息定制
    sendNotification(content,title='提示') {
        var that = this;
        console.log(this.currentNotifications,'currentNotificationscurrentNotifications')
        if (this.currentNotifications >= this.maxNotifications) {
          // 达到最大通知数量，不再发送
          this.$notification.warning({
            message: '提示',
            description: '错误提示信息已达到' + this.maxNotifications + '个 ，请先处理已提示的错误.',
            // duration: 5,
          });
        } else {
          // 发送通知，并增加当前通知数量
        //   const key = `notification-${this.currentNotifications}`;
        this.$notification.error({
            // key,
            duration:null,
            message: title,
            description: content,
            onClose:function(){
                that.currentNotifications--;
            }
          });
          this.currentNotifications++;
        }
    },
  
  }
}

export  { yaodianFormMinxin }